import os

from flask import Blueprint, render_template

index = Blueprint('index', __name__)


@index.route('/index')
def my_index():
    return '我是首页'


@index.route('/index2')
def my_index2():
    str = """
        <!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>实现定时页面跳转</title>
		<style>
			span {
				display: inline-block;
				width: 200px;
				height: 200px;
				line-height: 200px;
				text-align: center;
				background-color: black;
				color: white;
				font-size: 50px;
			}
			
		</style>
	</head>
	<body>
		<button>点我跳转慕课网</button>
		<span>5</span>
		<script>
			var imooc = document.querySelector('button');
			var mySpan = document.querySelector('span');
			imooc.addEventListener('click',function(){
				// console.log(location.href);
				location.href = "http://www.imooc.com";
			})
			// 页面的自动跳转
			var timer = 5;
			setInterval(function(){
				if(timer == 0){
					location.href = "http://www.imooc.com";
				}else {
					mySpan.innerHTML = timer;
					timer--;
				}
			},1000);
		</script>
	</body>
</html>
    """
    return str


@index.route('/index3')
def my_index3():
    username = 'dashi'
    return '<strong>{}</strong>'.format(username)


@index.route('/index4')
def my_index4():
    str = """
        <!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>实现定时页面跳转</title>
		<style>
			span {
				display: inline-block;
				width: 200px;
				height: 200px;
				line-height: 200px;
				text-align: center;
				background-color: black;
				color: white;
				font-size: 50px;
			}

		</style>
	</head>
	<body>
		<button>点我跳转慕课网</button>
		<span>{{timer}}</span>
		<script>
			var imooc = document.querySelector('button');
			var mySpan = document.querySelector('span');
			imooc.addEventListener('click',function(){
				// console.log(location.href);
				location.href = "http://www.imooc.com";
			})
			// 页面的自动跳转
			var timer = {{timer}};
			setInterval(function(){
				if(timer == 0){
					location.href = "http://www.imooc.com";
				}else {
					mySpan.innerHTML = timer;
					timer--;
				}
			},1000);
		</script>
	</body>
</html>
    """
    str = str.replace('{{timer}}', '10')
    return str


@index.route('/index5')
def my_index5():
    current_directory = os.getcwd()
    print(current_directory)
    with open('template/index.html', encoding='utf-8') as file:
        html = file.read()
    html = html.replace('{{timer}}', '10')
    return html


@index.route('/index6')
def my_index6():
    t = 7
    return render_template('index.html', timer=t)
